from flask import Blueprint, render_template, redirect, url_for, flash, request, current_app
from flask_login import login_required, current_user
from werkzeug.utils import secure_filename
from app import db
from app.models import Activity, Course, Organization
from app.forms import ActivityForm, CourseForm, OrganizationForm
from app.utils import allowed_file, save_uploaded_file
import os

content_bp = Blueprint('content', __name__)

@content_bp.route('/activities')
@login_required
def manage_activities():
    page = request.args.get('page', 1, type=int)
    activities = Activity.query.filter_by(user_id=current_user.id).order_by(
        Activity.created_at.desc()).paginate(
            page=page, per_page=current_app.config['ITEMS_PER_PAGE'], error_out=False)
    return render_template('content/activities.html', activities=activities)

@content_bp.route('/activity/new', methods=['GET', 'POST'])
@login_required
def new_activity():
    form = ActivityForm()
    if form.validate_on_submit():
        activity = Activity(
            title=form.title.data,
            description=form.description.data,
            content=form.content.data,
            location=form.location.data,
            region=form.region.data,
            start_time=form.start_time.data,
            end_time=form.end_time.data,
            max_participants=form.max_participants.data,
            is_featured=form.is_featured.data,
            status=form.status.data,
            user_id=current_user.id
        )
        
        if form.image.data:
            filename = save_uploaded_file(form.image.data, 'activities')
            activity.image_url = filename
        
        db.session.add(activity)
        db.session.commit()
        flash('活动已创建', 'success')
        return redirect(url_for('content.manage_activities'))
    
    return render_template('content/activity_form.html', form=form, title='创建新活动')

@content_bp.route('/activity/<int:id>/edit', methods=['GET', 'POST'])
@login_required
def edit_activity(id):
    activity = Activity.query.get_or_404(id)
    if activity.user_id != current_user.id and not current_user.is_admin():
        flash('您没有权限编辑此活动', 'danger')
        return redirect(url_for('content.manage_activities'))
    
    form = ActivityForm(obj=activity)
    if form.validate_on_submit():
        activity.title = form.title.data
        activity.description = form.description.data
        activity.content = form.content.data
        activity.location = form.location.data
        activity.region = form.region.data
        activity.start_time = form.start_time.data
        activity.end_time = form.end_time.data
        activity.max_participants = form.max_participants.data
        activity.is_featured = form.is_featured.data
        activity.status = form.status.data
        
        if form.image.data:
            filename = save_uploaded_file(form.image.data, 'activities')
            activity.image_url = filename
        
        db.session.commit()
        flash('活动已更新', 'success')
        return redirect(url_for('content.manage_activities'))
    
    return render_template('content/activity_form.html', form=form, title='编辑活动')

@content_bp.route('/activity/<int:id>/delete', methods=['POST'])
@login_required
def delete_activity(id):
    activity = Activity.query.get_or_404(id)
    if activity.user_id != current_user.id and not current_user.is_admin():
        flash('您没有权限删除此活动', 'danger')
        return redirect(url_for('content.manage_activities'))
    
    db.session.delete(activity)
    db.session.commit()
    flash('活动已删除', 'success')
    return redirect(url_for('content.manage_activities'))

# 类似的课程和机构管理路由...